﻿using DotNetUI.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DotNetUI.Controls
{
    /// <summary>
    /// 取色控件
    /// </summary>
    public class DotNetColorPicker : Control
    {

        static DotNetColorPicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DotNetColorPicker), new FrameworkPropertyMetadata(typeof(DotNetColorPicker)));
        }
        /// <summary>
        /// 获取模板时调用
        /// </summary>
        public override async void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var itemsControl=(ItemsControl)  this.GetTemplateChild("_items");
            var _standard = (ItemsControl)this.GetTemplateChild("_standard");
            var _system = (ItemsControl)this.GetTemplateChild("_system");
            _standard.PreviewMouseMove += ItemsControl_PreviewMouseMove;
            _standard.PreviewMouseDown += ItemsControl_PreviewMouseMove;
            _system.PreviewMouseMove += ItemsControl_PreviewMouseMove;
            _system.PreviewMouseDown += ItemsControl_PreviewMouseMove;

            itemsControl.PreviewMouseMove += ItemsControl_PreviewMouseMove;
            itemsControl.PreviewMouseDown += ItemsControl_PreviewMouseMove;
            List<Color> colors = new List<Color>();
            for (int i = 360; i >= 0; i--)
            {
                colors.Add(ColorHelper.Hsb2Rgb(new Hsb { Hue = i, Brightness = 100, Saturation = 100 }));
            }
            SliderColors = colors.ToArray();
            GenerateColor(Hue);
            var standardColors=  typeof(Colors).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
            StandardColors = standardColors.Select(p => (Color)p.GetValue(null)).ToArray();
            var systemColors = typeof(SystemColors).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).Where(p=>p.PropertyType==typeof(Color));
            SystemColors = systemColors.Select(p => (Color)p.GetValue(null)).ToArray();
        }
        /// <summary>
        /// 生成颜色
        /// </summary>
        /// <param name="hue">色相饱和度</param>
        /// <returns></returns>
        private void GenerateColor(double hue)
        {
            this.SetValue(ColorDictionaryProperty, GetColors(hue));
        }
        private IEnumerable<Color> GetColors(double hue)
        {
            var step = ColorStep;
            for (int b = 100 - 1; b >= 0; b -= step)
            {
                for (int s = 0; s < 100; s += step)
                {
                    Hsb hsb = new Hsb()
                    {
                        Hue = hue,
                        Brightness = b,
                        Saturation = s
                    };
                    yield return ColorHelper.Hsb2Rgb(hsb);
                }
            }
        }
        /// <summary>
        /// 引发颜色修改
        /// </summary>
        private  void RaisedColorChanged()
        {
             GenerateColor(Hue);
        }
        /// <summary>
        /// 色相
        /// </summary>
        public static readonly DependencyProperty HueProperty = DependencyProperty.Register(nameof(Hue), typeof(double), typeof(DotNetColorPicker),new PropertyMetadata(0.0D, (s, e) =>
        {
            if (s is DotNetColorPicker picker)
            {
                picker.RaisedColorChanged();
            }

        }));
        /// <summary>
        /// 色相
        /// </summary>
        public double Hue { get=>this.GetValue<double>(HueProperty); set=>SetValue(HueProperty,value); }
        /// <summary>
        /// 色差
        /// </summary>
        public static readonly DependencyProperty ColorStepProperty = DependencyProperty.Register(nameof(ColorStep),typeof(int),typeof(DotNetColorPicker),new PropertyMetadata(4, (s, e) => 
        {
            if (s is DotNetColorPicker picker)
            {
                picker.RaisedColorChanged();
            }
        }));
        /// <summary>
        /// 色差
        /// </summary>
        public int ColorStep { get=>this.GetValue<int>(ColorStepProperty); set=>SetValue(ColorStepProperty,value); }
        /// <summary>
        /// 取色字典
        /// </summary>

        public static readonly DependencyProperty ColorDictionaryProperty = DependencyProperty.Register(nameof(ColorDictionary), typeof(IEnumerable<Color>),typeof(DotNetColorPicker));
        /// <summary>
        /// 取色字典
        /// </summary>
        public IEnumerable<Color> ColorDictionary { get=>this.GetValue<IEnumerable<Color>>(ColorDictionaryProperty);   }
        /// <summary>
        /// 滑块的颜色
        /// </summary>
        public Color[] SliderColors { get; private set; }
        /// <summary>
        /// 当前选中的颜色
        /// </summary>
        public static readonly DependencyProperty SelectColorProperty = DependencyProperty.Register(nameof(SelectColor),typeof(Brush),typeof(DotNetColorPicker),new PropertyMetadata(null, (s, e) =>
        {
            if (s is DotNetColorPicker picker&& e.NewValue is SolidColorBrush brush)
            {
                picker.RaisedColorChanegd(new InnerColor(brush.Color));
            }
        }));
        private void RaisedColorChanegd(InnerColor innerColor)
        {
            InnerColor = innerColor;
        }
        /// <summary>
        /// 取色属性
        /// </summary>
        public static readonly DependencyProperty InnerColorProperty = DependencyProperty.Register(nameof(InnerColor),typeof(InnerColor),typeof(DotNetColorPicker),new PropertyMetadata(new InnerColor(Colors.Transparent)));
        /// <summary>
        /// 取色属性
        /// </summary>
        public InnerColor InnerColor { get=>this.GetValue<InnerColor>(InnerColorProperty); set=> SetValue(InnerColorProperty, value); }
        /// <summary>
        /// 当前选中的颜色
        /// </summary>
        public Brush SelectColor { get=>this.GetValue<Brush>(SelectColorProperty); set=>SetValue(SelectColorProperty,value); }

        private void ItemsControl_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var element = Mouse.DirectlyOver;
                if (element is Border border)
                {
                    SelectColor = (SolidColorBrush)border.Background;
                }
            }
        }
        /// <summary>
        /// 标准色
        /// </summary>

        public Color[] StandardColors { get; private set; }
        /// <summary>
        /// 系统色
        /// </summary>
        public Color[] SystemColors { get; private set; }
    }

}
